<html>
  <head>
    <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>GitHub Actions 自动部署实践 - Rubin&#39;s Blog</title>
<meta name="description" content="温故而知新" />
<link rel="shortcut icon" href="https://jrb1995.gitee.io/favicon.ico?v=1588066216187">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0/katex.min.css">
<link href="https://cdn.jsdelivr.net/npm/remixicon@1.3.1/fonts/remixicon.css" rel="stylesheet">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Droid+Serif:400,700">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css">

<link rel="stylesheet" href="https://jrb1995.gitee.io/styles/main.css">
<link rel="alternate" type="application/atom+xml" title="GitHub Actions 自动部署实践 - Rubin&#39;s Blog - Atom Feed" href="https://jrb1995.gitee.io/atom.xml">



  </head>
  <body>
    <div id="app" class="main px-4 flex flex-col lg:flex-row">
      <div id="sidebar" class="sidebar-wrapper lg:static lg:w-1/4">
  <div class="lg:sticky top-0">
    <div class="sidebar-content">
      <div class="flex lg:block p-4 lg:px-0 items-center fixed lg:static lg:block top-0 right-0 left-0 bg-white z-50">
        <i class="remixicon-menu-2-line lg:mt-4 text-2xl cursor-pointer animated fadeIn" onclick="openMenu()"></i>
        <a href="https://jrb1995.gitee.io">
          <img class="animated fadeInLeft avatar rounded-lg mx-4 lg:mt-32 lg:mx-0 mt-0 lg:w-24 lg:h-24 w-12 w-12" src="https://jrb1995.gitee.io/images/avatar.png?v=1588066216187" alt="">
        </a>
        <h1 class="animated fadeInLeft lg:text-4xl font-extrabold lg:mt-8 mt-0 text-xl" style="animation-delay: 0.2s">Rubin&#39;s Blog</h1>
      </div>
      
        <div class="animated fadeInLeft" style="animation-delay: 0.4s">
          <p class="my-4 text-gray-600 font-light hidden lg:block">
            文章目录
          </p>
          <div class="toc-container hidden lg:block">
            <ul class="markdownIt-TOC">
<li>
<ul>
<li><a href="#%E5%89%8D%E8%A8%80">前言</a></li>
<li><a href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE">创建项目</a></li>
<li><a href="#%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9C">准备工作</a></li>
<li><a href="#%E7%BC%96%E5%86%99-yml-%E6%96%87%E4%BB%B6">编写 yml 文件</a></li>
<li><a href="#%E6%9C%80%E5%90%8E%E5%B7%A5%E4%BD%9C">最后工作</a></li>
</ul>
</li>
</ul>

          </div>
        </div>
      
    </div>
  </div>
</div>

<div class="menu-container">
  <i class="remixicon-arrow-left-line text-2xl cursor-pointer animated fadeIn close-menu-btn" onclick="closeMenu()"></i>
  <div>
    
      
        <a href="/" class="menu" style="animation-delay: 0s">
          首页
        </a>
      
    
      
        <a href="/archives" class="menu" style="animation-delay: 0.2s">
          归档
        </a>
      
    
      
        <a href="/tags" class="menu" style="animation-delay: 0.4s">
          标签
        </a>
      
    
      
        <a href="/post/about" class="menu" style="animation-delay: 0.6000000000000001s">
          关于
        </a>
      
    
  </div>
  <div class="site-footer">
    <div class="py-4 text-gray-700">Powered by <a href="https://github.com/getgridea/gridea" target="_blank">Gridea</a></div>
    <a class="rss" href="https://jrb1995.gitee.io/atom.xml" target="_blank">RSS</a>
  </div>
</div>
<div class="mask" onclick="closeMenu()">
</div>
      <div class="content-wrapper py-32 lg:p-8 lg:w-3/4 post-detail animated fadeIn">
        <h1 class="text-3xl font-bold lg:mt-16">GitHub Actions 自动部署实践</h1>
        <div class="text-sm text-gray-700 lg:my-8">
          2020-02-13 / 3 min read
        </div>
        
          <img class="post-feature-image rounded-lg mx-auto my-4" src="https://jrb1995.gitee.io/post-images/github-actions-zi-dong-bu-shu-shi-jian.jpeg" alt="">
        
        <div class="post-content yue">
          <h2 id="前言">前言</h2>
<p>GitHub Actions 是 GitHub 推出的持续集成服务，语法介绍可以看 <a href="https://github.com/features/actions" target="_blank">官方文档</a> 或者 <a href="http://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html" target="_blank">阮一峰老师的博客</a>。于是本着折腾的心思来自动发布一个 vue 应用到自己的 GitHub Pages。</p>
<h2 id="创建项目">创建项目</h2>
<p>使用 <a href="https://cli.vuejs.org/zh/" target="_blank">vue-cli</a> 创建一个简单的 vue 项目</p>
<pre><code>vue create github-actions-demo
</code></pre>
<blockquote>
<p>Vue Router 不要选择 History 模式，发布到 github page 是一个纯前端静态页面，而 History 模式需要后台配置支持。</p>
</blockquote>
<p>项目创建完成后，还需要在 <code>vue.config.js</code> 中设置正确的 <code>publicPath</code>。</p>
<p>如果打算将项目部署到 <code>https://&lt;USERNAME&gt;.github.io/</code> 上，<code>publicPath</code> 默认为 <code>&quot;/&quot;</code> 可以忽略这个参数。</p>
<p>如果打算将项目部署到 <code>https://&lt;USERNAME&gt;.github.io/&lt;REPO&gt;/</code> 上（即仓库地址为 <code>https://github.com/&lt;USERNAME&gt;/&lt;REPO&gt;</code>），将 <code>publicPath</code> 设为 <code>&quot;/&lt;REPO&gt;/&quot;</code>。举个例子，如果仓库名字为 “my-project”，那么 <code>vue.config.js</code> 的内容应如下所示：</p>
<pre><code class="language-js">module.exports = {
  publicPath: process.env.NODE_ENV === 'production'
    ? '/my-project/'
    : '/'
}
</code></pre>
<h2 id="准备工作">准备工作</h2>
<p>创建一个 GitHub 仓库，以及配置 GitHub 密钥。</p>
<ol>
<li>
<p>申请一个 access tokens</p>
<p>点击头像 <code>Setting</code> &gt; <code>Developer setttings</code> &gt; <code>Personal access tokens</code></p>
<figure data-type="image" tabindex="1"><img src="https://jrb1995.gitee.io/post-images/1581598562909.jpeg" alt="" loading="lazy"></figure>
<p>点击 <code>Generate new token</code> 按钮，勾选好权限生成 token，记下生成的 token 之后需要用到。</p>
</li>
<li>
<p>在创建的仓库中点击  <code>Settings</code> &gt; <code>Secrets</code> &gt; <code>Add a new secret</code></p>
<figure data-type="image" tabindex="2"><img src="https://jrb1995.gitee.io/post-images/1581598887444.png" alt="" loading="lazy"></figure>
<p>Name 随意填写，Value 填入刚申请的 token 值</p>
</li>
</ol>
<h2 id="编写-yml-文件">编写 yml 文件</h2>
<pre><code class="language-yml">name: GitHub Actions Build and Deploy Demo
on:
  push:
    branches:
      - master # 整个流程在master分支发生push事件时触发
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@master

    - name: Build and Deploy
      uses: JamesIves/github-pages-deploy-action@releases/v2
      env:
        ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} # 密钥
        BRANCH: gh-pages # 发布分支
        FOLDER: dist # vue-cli build 后的目录
        BUILD_SCRIPT: npm install &amp;&amp; npm run build # 构建脚本
</code></pre>
<h2 id="最后工作">最后工作</h2>
<p>保存文件后，将整个仓库推送到 GitHub，完整的 <a href="https://github.com/jrb1995/github-actions-demo" target="_blank">代码仓库</a> 在这里。</p>
<p>Actions 开始自动运行，接着等待它完成。</p>
<figure data-type="image" tabindex="3"><img src="https://jrb1995.gitee.io/post-images/1581599890441.jpeg" alt="" loading="lazy"></figure>
<p>等 workflow 运行结束，访问 <a href="https://jrb1995.github.io/github-actions-demo/#/" target="_blank">GitHub Page</a> 就可以看到页面了。</p>
<figure data-type="image" tabindex="4"><img src="https://jrb1995.gitee.io/post-images/1581600153643.png" alt="" loading="lazy"></figure>
<p>以后，每次修改后推送源码，GitHub Actions 都会自动运行，将构建产物发布到网页。</p>
<blockquote>
<p>GitHub Page 会有生效时间，在 Settings 中 GitHub Pages 看到配置如下时就可以正常访问了。</p>
</blockquote>
<figure data-type="image" tabindex="5"><img src="https://jrb1995.gitee.io/post-images/1581600456035.png" alt="" loading="lazy"></figure>

        </div>

        
          <a class="animated fadeInUp p-2 items-center text-sm text-gray-700 border hover:bg-gray-300 leading-none rounded-full flex lg:inline-flex m-4 " href="https://jrb1995.gitee.io/tag/CIqKkHltz/">
            <span class="flex-auto">github</span>
          </a>
        
          <a class="animated fadeInUp p-2 items-center text-sm text-gray-700 border hover:bg-gray-300 leading-none rounded-full flex lg:inline-flex m-4 " href="https://jrb1995.gitee.io/tag/FtY66XQIw/">
            <span class="flex-auto">git</span>
          </a>
        


        <div class="flex justify-between py-8">
          
            <div class="prev-post">
              <a href="https://jrb1995.gitee.io/post/vue-yuan-ma-fen-xi-xiang-ying-shi-yuan-li/">
                <h3 class="post-title">
                  <i class="remixicon-arrow-left-line"></i>
                  Vue 源码分析 — 响应式原理
                </h3>
              </a>
            </div>
          

          
            <div class="next-post">
              <a href="https://jrb1995.gitee.io/post/git-commit-message-gui-fan-hua-shi-jian/">
                <h3 class="post-title">
                  Git Commit Message 规范化实践
                  <i class="remixicon-arrow-right-line"></i>
                </h3>
              </a>
            </div>
          
        </div>

        
          
            <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
<script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>

<div id="gitalk-container"></div>

<script>

  var gitalk = new Gitalk({
    clientID: '58415ac62e821b4b6bbd',
    clientSecret: '69a93aa48149a7a74c6517efbbb32c57bc07799f',
    repo: 'jrb1995.github.io',
    owner: 'jrb1995',
    admin: ['jrb1995'],
    id: (location.pathname).substring(0, 49),      // Ensure uniqueness and length less than 50
    distractionFreeMode: false  // Facebook-like distraction free mode
  })

  gitalk.render('gitalk-container')

</script>

          

          
        

      </div>
    </div>

    <script src="https://jrb1995.gitee.io/media/prism.js"></script>  
<script>

Prism.highlightAll()

let mainNavLinks = document.querySelectorAll(".markdownIt-TOC a");

// This should probably be throttled.
// Especially because it triggers during smooth scrolling.
// https://lodash.com/docs/4.17.10#throttle
// You could do like...
// window.addEventListener("scroll", () => {
//    _.throttle(doThatStuff, 100);
// });
// Only not doing it here to keep this Pen dependency-free.

window.addEventListener("scroll", event => {
  let fromTop = window.scrollY;

  mainNavLinks.forEach((link, index) => {
    let section = document.getElementById(decodeURI(link.hash).substring(1));
    let nextSection = null
    if (mainNavLinks[index + 1]) {
      nextSection = document.getElementById(decodeURI(mainNavLinks[index + 1].hash).substring(1));
    }
    if (section.offsetTop <= fromTop) {
      if (nextSection) {
        if (nextSection.offsetTop > fromTop) {
          link.classList.add("current");
        } else {
          link.classList.remove("current");    
        }
      } else {
        link.classList.add("current");
      }
    } else {
      link.classList.remove("current");
    }
  });
});


document.addEventListener("DOMContentLoaded", function() {
  var lazyImages = [].slice.call(document.querySelectorAll(".post-feature-image.lazy"));

  if ("IntersectionObserver" in window) {
    let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
      entries.forEach(function(entry) {
        if (entry.isIntersecting) {
          let lazyImage = entry.target
          lazyImage.style.backgroundImage = `url(${lazyImage.dataset.bg})`
          lazyImage.classList.remove("lazy")
          lazyImageObserver.unobserve(lazyImage)
        }
      });
    });

    lazyImages.forEach(function(lazyImage) {
      lazyImageObserver.observe(lazyImage)
    })
  } else {
    // Possibly fall back to a more compatible method here
  }
});

const menuContainer = document.querySelector('.menu-container')
const menus = document.querySelectorAll('.menu-container .menu')
const mask = document.querySelector('.mask')
const contentWrapper = document.querySelector('.content-wrapper')
const latestArticle = document.querySelector('.latest-article')
const readMore = document.querySelector('.read-more')
const indexPage = document.querySelector('.index-page')

const isHome = location.pathname === '/'
if (latestArticle) {
  latestArticle.style.display = isHome ? 'block' : 'none'
  readMore.style.display = isHome ? 'block' : 'none'
  indexPage.style.display = isHome ? 'none' : 'block'
}

const openMenu = () => {
  menuContainer.classList.add('open')
  menus.forEach(menu => {
    menu.classList.add('animated', 'fadeInLeft')
  })
  mask.classList.add('open')
  contentWrapper.classList.add('is-second')
}

const closeMenu = () => {
  menuContainer.classList.remove('open')
  menus.forEach(menu => {
    menu.classList.remove('animated', 'fadeInLeft')
  })
  mask.classList.remove('open')
  contentWrapper.classList.remove('is-second')
}
</script>
  
  </body>
</html>
